<?php
namespace Tongcheng\Controller;
use Think\Controller;
class CardController extends CommonController {
    public function index(){
    	$model = M("card_pics");
    	$map['wid'] = session('wid');
    	$list = $model->where($map)->select();
        $this->assign('list',$list);
        $this->display();
    }
    //更新使用状态
    public function up_used(){
        $model = M("card");
        $map['wid'] = session('wid');
        $map['id'] = I('id');
        $info = $model->where($map)->find();
        $data['used'] =  ($info['used'] == 1)?0:1;
        //$data['used'] = I('used');
        //仅显示一个。修改其他状态
        if($data['used'] == 1){
            M('card')->where(['wid'=>session('wid')])->save(['used'=>0]);
        }
        $re = $model->where($map)->save($data);
        if($re){
            $this->success('修改成功！');
        }else{
            $this->error('网络忙，请稍后再试！');
        }
    }
    //更新使用状态
    public function up_status(){
        $model = M("card_pics");
        $map['wid'] = session('wid');
        $map['id'] = I('id');
        $info = $model->where($map)->find();
        $data['status'] =  ($info['status'] == 1)?0:1;
        //$data['used'] = I('used');
        //仅显示一个。修改其他状态
        if($data['status'] == 1){
            M('card_pics')->where(['wid'=>session('wid')])->save(['status'=>0]);
        }
        $re = $model->where($map)->save($data);
        if($re){
            $this->success('修改成功！');
        }else{
            $this->error('网络忙，请稍后再试！');
        }

    }
    //同城卡编辑
    public function card(){
        $model = M("card");
        $map['wid'] = session('wid');
        $list = $model->where($map)->select();
        $this->assign('list',$list);
        $this->display();
    }

    public function add(){
        if(IS_AJAX){
            $model = M("card_pics");
            $new['wid'] = session('wid');
            $new['title'] = I('post.title');
            $new['pic'] = I('post.imgurl');
            $new['status'] = 1;
            $new['create_time'] = date("Y-m-d H:i:s");
            if($model->add($new)){
                $this->success('添加成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $this->display();
        }
    }
    //同城卡编辑
    public function addCard(){
        if(IS_AJAX){
            $model = M("card");
            $new['wid'] = session('wid');
            //$new['pic'] = I('post.imgurl');
            $new['create_time'] = date("Y-m-d H:i:s");
            $new['title'] = I('title');
            $new['days'] = I('days');
            $new['price'] = I('price');
            $new['used'] = 1;
            $new['song_money'] = I('song_money');
            $new['song_money2'] = I('song_money2');
            $new['shop_discount'] = I('shop_discount');
            if(I('type')==1){
                $new['is_fee'] = 1;
                $new['tie_fee'] = I('tie_fee');//发帖优惠
            }
            elseif(I('type')==2){
                $new['is_num'] = 1;
                $new['tie_num'] = I('tie_num');//发帖优惠
            }
            if($model->add($new)){
                $this->success('添加成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $re = M('wechat')->where(['wid'=>session('wid')])->find();
            $this->assign('wechat',$re);
            $this->display();
        }
    }
    //同城卡编辑
    public function editCard(){
        $model = M("card");
        $map['wid'] = session('wid');
        $map['id'] = $_GET['id'];
        if(IS_AJAX){
            $new['pic'] = I('post.imgurl');
            $new['title'] = I('title');
            $new['days'] = I('days');
            $new['price'] = I('price');
            $new['song_money'] = I('song_money');
            $new['song_money2'] = I('song_money2');
            $new['shop_discount'] = I('shop_discount');
            if(I('type')==1){
                $new['is_fee'] = 1;
                $new['is_num'] = 0;
                $new['tie_fee'] = I('tie_fee');//发帖优惠
            }
            elseif(I('type')==2){
                $new['is_num'] = 1;
                $new['is_fee'] = 0;
                $new['tie_num'] = I('tie_num');//发帖优惠
            }
            $model->where($map)->save($new);
            $this->success('修改成功！');
        }else{
            $vo = $model->where($map)->find();
            $this->assign('vo',$vo);
            $re = M('wechat')->where(['wid'=>session('wid')])->find();
            $this->assign('wechat',$re);
            $this->display('addCard');
        }
    }
    //同城卡删除
    public function delCard(){
        $model = M("card");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }
    public function edit(){
    	$model = M("card_pics");
    	$map['wid'] = session('wid');
        $map['id'] = $_GET['id'];
        if(IS_AJAX){
            $new['title'] = I('post.title');
            $new['pic'] = I('post.imgurl');
            $model->where($map)->save($new);
            $this->success('修改成功！');
        }else{
            $vo = $model->where($map)->find();
            $this->assign('vo',$vo);
            $this->display('add');
        }
    }

    public function del(){
        $model = M("card_pics");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }

    //商家参与同城卡
    public function examineShopCard () {
        $where['a.wid'] = session('wid');
        $list = M('shop_card_yh')->alias('a')
                                 ->field('a.*, b.name as shopname')
                                 ->join('tp_shop b on a.shop_id = b.shop_id')
                                 ->where($where)
                                 ->order('a.status asc,a.sort_num desc, a.create_time desc')
                                 ->select();
        //特权日
        foreach ($list as $k => $v) {
            $dateNmae = [];
            if ($v['is_week'] == 1) {
                if ($v['week_0'] == 1) {
                    $dateNmae[] = '日';
                }
                if ($v['week_1'] == 1) {
                    $dateNmae[] = '一';
                }
                if ($v['week_2'] == 1) {
                    $dateNmae[] = '二';
                }
                if ($v['week_3'] == 1) {
                    $dateNmae[] = '三';
                }
                if ($v['week_4'] == 1) {
                    $dateNmae[] = '四';
                }
                if ($v['week_5'] == 1) {
                    $dateNmae[] = '五';
                }
                if ($v['week_6']  == 1) {
                    $dateNmae[] = '六';
                }
                $dateNmae = implode(',', $dateNmae);
            } elseif ($v['is_day'] == 1) {
                $dateNmae = $v['days'].'号';
            }
            $list[$k]['datename'] = $dateNmae;
        }
        $this->assign('list', $list);
        $this->display();
    }
    //通过审核
    public function up_examineShop () {
        $id = $_POST['id'];
        $state = $_POST['state'];
        $state == 1 ? $state = 0 : $state = 1;
        $row = M('shop_card_yh')->where(['id' => $id])->save(['status' => 1]);
        if ($row) {
            $this->ajaxReturn(['status' => 1, 'info' => '审核成功']);
        } else {
            $this->ajaxReturn(['status' => 0, 'info' => '网络繁忙']);
        }
    }

    //
    public function edit_examineShopCard () {
        $id = $_GET['id'];
        $list = M('shop_card_yh')->alias('a')
                                 ->field('a.*, b.hot_num')
                                 ->join('tp_shop b on a.shop_id = b.shop_id')
                                 ->where(['a.id' => $id])
                                 ->find();
        $this->assign('vo', $list);
        $this->display();
    }

    //
    public function save_examineShopCard () {
        //热度
        $rowShop = M('shop')->where(['shop_id' => $_POST['shop_id']])->save(['hot_num' => $_POST['hot_num']]);
        $rowYh = M('shop_card_yh')->where(['id' => $_POST['id']])->save(['count' => $_POST['count'], 'frequency' => $_POST['frequency'],'sort_num' => $_POST['sort_num']]);
        if ($rowShop && $rowYh) {
            $this->ajaxReturn(['status' => 1, 'info' => '修改成功']);
        } else {
            $this->ajaxReturn(['status' => 0, 'info' => '网络繁忙']);
        }
    }
}